<%@page import="kr.or.kosta.NaverImages"%>
<%@page import="net.sf.json.JSONArray"%>
<%@page import="org.jsoup.nodes.Element"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="org.jsoup.select.Elements"%>
<%@page import="org.jsoup.nodes.Document"%>
<%@page import="org.jsoup.Jsoup"%>
<%@page import="java.nio.charset.CharsetDecoder"%>
<%@page import="java.net.URLEncoder"%>
<%@page import="java.io.InputStreamReader"%>
<%@page import="java.io.BufferedReader"%>
<%@page import="java.io.InputStream"%>
<%@page import="java.net.HttpURLConnection"%>
<%@page import="java.net.URL"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	//System.out.println(request.getParameter("url"));
	String totalStr = "";
	try {
		List<NaverImages> imgArrays = new ArrayList<NaverImages>();
		String realLinkUrlprefix = "http://shopping.naver.com/search/ajax/rd.nhn?nvMid=";
		String realLinkUrlmidfix = "&prntExpsTrtrCd=SHSR&cpcExtrtrCd=SHSR02&bizCd=0301&nclickAreaCode=lst*C&frm=NVSHSRC&query=";
		String realLinkUrlsuffix = "";
		int maxProductSize = 0;
		Document doc = Jsoup.connect(request.getParameter("url")).get();
		
		// 멀티 사이트 검색
		// <img>태그 전체
		Elements images_MultiSite = doc.select("._model_list .img_area ._productLazyImg");
		// <a>태그 전체(실제 링크는 아니고 링크를 만들 정보 담고 있는 놈)
		Elements links_MultiSite = doc.select("._model_list .info_mall .mall_list li:first-child ._lowPriceByMall");
		
		// 단일 사이트 검색
		// <img>태그 전체
		Elements images_SingleSite = doc.select("._product_list .img_area ._productLazyImg");
		// <a>태그 전체
		Elements links_SingleSite = doc.select("._product_list .img_area .img");
		
		// 전체 품목 갯수
		Elements max_product_size = doc.select("._productSet_total");
		maxProductSize = Integer.parseInt(max_product_size.text().replace("전체", "").replace(",", ""));
		
		//System.out.println(max_product_size.text());
		
		if(images_MultiSite != null && images_MultiSite.size() > 0){
			for(int i = 0; i < images_MultiSite.size(); i++){
				Element multiSiteImage = images_MultiSite.get(i);
				Element multiSiteLink = links_MultiSite.get(i);
				
				NaverImages image = new NaverImages();
				image.setImageID(multiSiteLink.attr("nvMid"));
				image.setImageUrl(multiSiteImage.attr("src"));
				image.setLinkUrl(realLinkUrlprefix + multiSiteLink.attr("nvMid") + realLinkUrlmidfix + multiSiteLink.attr("query"));
				imgArrays.add(image);
			}
		}
		
		JSONArray jsonEmplist = JSONArray.fromObject(imgArrays);
 		out.print(jsonEmplist);
 		
	} catch (Exception e) {
		e.printStackTrace();
	}
%>